package com.quectel.business.dms.device;

import cn.hutool.core.lang.func.LambdaUtil;
import com.google.common.collect.Lists;
import com.quectel.business.dms.application.ApplicationBusiness;
import com.quectel.constant.core.entranceguard.EntranceGuardPersonConstants;
import com.quectel.core.module.device.dto.DeviceDto;
import com.quectel.core.module.device.service.DeviceCategoryService;
import com.quectel.core.module.device.service.DeviceService;
import com.quectel.core.module.device.service.DeviceTypeService;
import com.quectel.core.module.entranceguard.dto.EntranceGuardPersonDto;
import com.quectel.core.module.entranceguard.service.EntranceGuardPersonService;
import com.quectel.core.module.village.dto.VillageRoomDto;
import com.quectel.core.module.village.service.VillageRoomService;
import com.quectel.sys.module.sys.service.SysConfigService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class DeviceBusiness {

    @DubboReference
    private DeviceTypeService deviceTypeService;
    @DubboReference
    private DeviceCategoryService deviceCategoryService;
    @DubboReference
    private SysConfigService sysConfigService;
    @Autowired
    private ApplicationBusiness applicationBusiness;
    @DubboReference
    private EntranceGuardPersonService entranceGuardPersonService;
    @DubboReference
    private VillageRoomService villageRoomService;
    @DubboReference
    private DeviceService deviceService;

    private static final Logger LOGGER = LoggerFactory.getLogger(DeviceBusiness.class);

    /**
     * 根据人员获取已授权门禁设备
     *
     * @param personType 1住户 2访客 SystemConstants.STAFF or SystemConstants.VISITOR ...
     * @param personId   人员id
     * @return
     */
    public List<DeviceDto> queryAuthEntranceGuardByPerson(Byte personType, Long personId) {
        List<EntranceGuardPersonDto> entranceGuardPersonDtos = entranceGuardPersonService.queryAuthByPersonId(personType, personId);
        return entranceGuardPersonDtos.stream()
                .filter(o -> EntranceGuardPersonConstants.AuthTypeEnum.ADD.getAuthType().equals(o.getAuthType()))
                .filter(o -> EntranceGuardPersonConstants.AuthStatusEnum.SEND_SUCCESS.getAuthStatus().equals(o.getAuthStatus()))
                .map(EntranceGuardPersonDto::getDevice).collect(Collectors.toList());
    }
}
